package bibliotheque.nameserver.parser;

import att.grappa.Graph;
import att.grappa.GrappaPanel;
import att.grappa.Parser;
import javax.swing.*;
import java.awt.*;
import java.io.*;


public class Graphe extends JScrollPane{
    
    private StringBuilder sBuilder = new StringBuilder();
    
    public Graphe(StringBuilder sBuilder ) {
        
        
        /*
         * complete the StringBuilder which contains the graph
         **/
        
        this.sBuilder = sBuilder;
        
        /*
         * get a runtime to execute DOT program
         **/
        Runtime mRuntime = Runtime.getRuntime();
        Process mProc = null;
        try {
            mProc = mRuntime.exec("dot");
        } catch (IOException ex) {
            try {
                mProc = mRuntime.exec("dot.exe");
            } catch (IOException ex2) {
                ex2.printStackTrace();
            }
        }
        
        /*
         * get the output stream to write the graph for DOT
         **/
        DataOutputStream dataoutputstream = new DataOutputStream(mProc.getOutputStream());
        try {
            dataoutputstream.writeBytes(sBuilder.toString());
            dataoutputstream.flush();
            dataoutputstream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        /*
         * get the input stream to get the good graph with the position
         **/
        InputStream inputstream = mProc.getInputStream();
        
        /*
         * create a new Parser to parse the graph
         **/
        Parser parser = new Parser(inputstream);
        try {
            parser.parse();
        } catch (Exception e) {
            System.err.println(e);
            System.exit(0);
        }
        
        /*
         * create a new GrappaPanel to display the graph, use getGraph() from parser to get the Graph
         **/
        GrappaPanel gp = null;
        Graph graph = parser.getGraph();
        gp = new GrappaPanel(graph);
        gp.setScaleToFit(false);
        
        /*
         * create a JScrollPane to have ScrollBar to display big graph
         **/
        //JScrollPane jsp = new JScrollPane(gp);
        this.setViewportView(gp);
        this.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
        //this.setViewportView(gp);
        
        /*
         * place the JScrollPane into the frame in the center
         **/
        //this.add(jsp,BorderLayout.CENTER);
        
        /*
         * fix the position top-left corner (0,0) and the size (600,600)
         */
        //this.setBounds(0,0,600,600);
        
        /*
         * set the default close operation, here, exit application
         **/
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        /*
         * to conclude setVisible at true to display the frame
         **/
        //this.setVisible(true);
        
    }
    
    public static void main(String[] args) {
        //Graphe mainWindow = new Graphe();
    }
}